package com.example.leaderapplication.View.Fragment;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.OnBackPressedCallback;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leaderapplication.Adapter.MyItemAdapter;
import com.example.leaderapplication.Base.BaseConfig;
import com.example.leaderapplication.Base.BaseFragment;
import com.example.leaderapplication.Bean.DataBean;
import com.example.leaderapplication.R;
import com.example.leaderapplication.Utils.SPUtil;
import com.example.leaderapplication.Utils.ToastUtil;
import com.example.leaderapplication.View.Activity.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class MyFragment extends BaseFragment {
    protected View rootView;

    private static MyFragment homePageFragment;
    private RecyclerView myItem;
    private TextView myName;
    private TextView myWelcome;
    private Button quitApp;
    private List<String> dateItem;
    private MyItemAdapter itemAdapter;

    public static MyFragment getInstance() {
        if (homePageFragment == null) {
            homePageFragment = new MyFragment();
        }
        return homePageFragment;
    }

    @Override
    public View setView(LayoutInflater inflater) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_my, null);

        }
        return rootView;
    }

    @Override
    public void initView() {

        myName = (TextView) view.findViewById(R.id.tv_my_name);
        myWelcome = (TextView) view.findViewById(R.id.tv_my_welcome);
        quitApp = (Button) view.findViewById(R.id.bt_quit_app);

        myItem = (RecyclerView) view.findViewById(R.id.rv_my_item);
        // 设置布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        myItem.setLayoutManager(linearLayoutManager);
        itemAdapter = new MyItemAdapter(getContext(), DataBean.getMyItem());
        myItem.setAdapter(itemAdapter);

        myName.setText("ID: " + SPUtil.get(getContext(), BaseConfig.USER_NAME,""));
    }

    @Override
    public void initData() {
        quitApp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), LoginActivity.class));
                getActivity().finish();
                System.exit(0);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_BACK) {
//                    startActivity(new Intent(getContext(), LoginActivity.class));
//                    getActivity().finish();
//                    System.exit(0);
                    return true;
                }
                return false;
            }
        });

    }
}
